package com.atguigu.exam;

import java.util.*;

/**
 * 如何遍历Map的key集，value集,key-value集，使用上泛型
 * @author xbd
 * @create 2021-05-29 16:03
 */
public class MapTest {
    public static void main(String[] args) {
        HashMap<String, Integer> map = new HashMap<>();
        map.put("席清鸥",23);
        map.put("大剿匪",18);
        map.put("席梦思",36);
        map.put("飘柳絮",48);

        //遍历Key
        Set<String> keySet = map.keySet();
        for(String key : keySet){
            System.out.println(key);
        }

        //遍历value
        Collection<Integer> values = map.values();
        Iterator<Integer> iterator = values.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }

        //遍历key-value
        Set<Map.Entry<String, Integer>> entries = map.entrySet();
        Iterator<Map.Entry<String, Integer>> iterator1 = entries.iterator();
        while (iterator1.hasNext()){
            Map.Entry<String, Integer> entry = iterator1.next();
            String key = entry.getKey();
            Integer value = entry.getValue();
            System.out.println(key + "---->" + value);
        }
    }
}
